import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

abstract public class ItemEditor<T>
{
    public ItemEditor(Judicator k, Shell parent, String title, T target,
                      int panelWidth, int panelHeight)
    {
        judicator = k;
        this.target = target;

        shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);

        ok = new Button(shell, SWT.PUSH);
        cancel = new Button(shell, SWT.PUSH);

        ok.setText("ok");
        ok.setBounds(60,panelHeight,60,30);

        cancel.setText("cancel");
        cancel.setBounds(155,panelHeight,60,30);

        _init();

        if (target == null)
        {
            ok.addSelectionListener(new SelectionAdapter()
            {   public void widgetSelected(SelectionEvent e) {_addItem();}});
        }
        else
        {
            ok.addSelectionListener(new SelectionAdapter()
            {   public void widgetSelected(SelectionEvent e) {_modifyItem();}});

            _assumeData();
        }

        cancel.addSelectionListener(new SelectionAdapter()
        {   public void widgetSelected(SelectionEvent e) {close();}});

        shell.setDefaultButton(ok);
        
        shell.setText(title);
        shell.setMinimumSize(panelWidth, panelHeight + 70);
        shell.pack();
        shell.open();
    }


    Shell shell;
    Button ok, cancel;

    Judicator judicator;
    T target;

    abstract protected void _init();
    abstract protected void _assumeData();
    abstract protected void _addItem();
    abstract protected void _modifyItem();
    abstract protected void _close();

    public void close()
    {
        _close();
        shell.dispose();
        judicator.saveProgress();
    }
}
